import os
import cv2 as cv
import glob


# 传入的是 子文件夹路径
def imgConversion(inputpath, outputpath, mode=None):
    # if os.path.exists(outputpath) == False:  # 判断目标 文件/目录是否存在
    #     os.makedirs(outputpath)  # 创建目录
    print("读取路径：", inputpath)
    (path, typename) = os.path.split(inputpath)  # 分割路径和文件名(目录名)
    name_list = []

    for dirpath, _, filenames in os.walk(inputpath):  # 返回元组：当前文件夹路径、子目录列表、子文件列表
        for filename in filenames:  # 子文件夹子目录列表
            # name_list.append(current_path + "/" + filename)
            name_list.append(os.path.join(dirpath, filename))  # 全限定路径 = 目录名+文件名

    count = len(name_list)
    if count > 0:
        # 遍历读取每一个子文件夹的子文件
        for i in range(count):
            img = cv.imread(name_list[i])
            # dst = cv2.resize(img, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_NEAREST)  # 调整大小
            # 调整图像格式
            filename = typename + "_{}".format(i) + '.png'
            print(os.path.join(outputpath, filename))
            cv.imwrite(os.path.join(outputpath, filename), img)
            print(i, "/", count)
        print("done！")
    else:
        print("文件夹无子文件")


# 对文件夹内的图像进行操作
def currentConversion(current_path):
    k = 0
    n = 1
    typename = current_path.split("\\")[-1]  # 读取文件夹名作为类型名

    fullyPath = current_path + "\\" + "*.bmp"
    images = glob.glob(fullyPath)  # 通配符* 读取所有jpg文件
    for frame in images:  # images类型是列表[]
        input_img = cv2.imread(frame)
        # output_img = cv2.resize(input_img, None, fx=0.8, fy=0.8, interpolation=cv.INTER_CUBIC)

        filename = typename + "_{}_{}".format(n, k) + '.png'
        cv2.imwrite(os.path.join(current_path, filename), input_img)  # 保存图像
        # 若是多张照片一组，组别参数n
        if k == 3:
            k = 0
            n += 1
        else:
            k += 1
    print("全部完成！")


# 对子文件夹内-子文件夹的图像进行操作
def totalConversion(master_path):
    for dirpath, dirnames, filenames in os.walk(master_path):  # 返回元组：目录路径、子目录列表、子文件列表
        for dirname in dirnames:  # 子目录列表
            fullyPath = os.path.join(master_path, dirname)  # 每一个子文件夹路径
            imgConversion(fullyPath, fullyPath)
    print("全部完成！")


# 对文件夹内的图像进行格式、大小转换
def FormatSize(path):
    datanames = os.listdir(path)  # 输出路径下所有文件的文件名(包括文件夹)
    count = 0
    for fullfilename in datanames:
        filename, extension = os.path.splitext(fullfilename)[0], os.path.splitext(fullfilename)[-1]
        if extension == '.bmp':  # 以路径最后一个'/'或者'\\'为分隔，返回路径与文件名的元祖，若只有文件名，则输出文件名与空。
            imgPath = os.path.join(path, fullfilename)  # 原文件名
            input_img = cv.imread(imgPath)
            output_img = cv.resize(input_img, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_CUBIC)
            # 格式转换
            filenameNew = filename + '.png'
            writePath = os.path.join(path, filenameNew)
            cv.imwrite(writePath, output_img)
            count += 1
    print("All Done！Sum：", count)


masterPath = r'F:\MVS_Data\20220909\A4\VS'  # 不能使用中文路径
# totalConversion(master_path)
# currentConversion(master_path)
# FormatSize(masterPath)



